package cn.ld.module.system.dal.mapper.dict;

import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.mybatis.core.mapper.BaseMapperX;
import cn.ld.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ld.module.system.controller.admin.dict.vo.data.DictDataPageInVO;
import cn.ld.module.system.dal.entity.dict.DictDataEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Mapper
public interface DictDataMapper extends BaseMapperX<DictDataEntity> {

    default DictDataEntity selectByDictTypeAndValue(String dictType, String value) {
        return selectOne(DictDataEntity::getDictType, dictType, DictDataEntity::getValue, value);
    }

    default DictDataEntity selectByDictTypeAndLabel(String dictType, String label) {
        return selectOne(DictDataEntity::getDictType, dictType, DictDataEntity::getLabel, label);
    }

    default List<DictDataEntity> selectByDictTypeAndValues(String dictType, Collection<String> values) {
        return selectList(new LambdaQueryWrapper<DictDataEntity>().eq(DictDataEntity::getDictType, dictType)
                .in(DictDataEntity::getValue, values));
    }

    default long selectCountByDictType(String dictType) {
        return selectCount(DictDataEntity::getDictType, dictType);
    }

    default PageResult<DictDataEntity> selectPage(DictDataPageInVO inVO) {
        return selectPage(inVO, new LambdaQueryWrapperX<DictDataEntity>()
                .likeIfPresent(DictDataEntity::getLabel, inVO.getLabel())
                .eqIfPresent(DictDataEntity::getDictType, inVO.getDictType())
                .eqIfPresent(DictDataEntity::getStatus, inVO.getStatus())
                .orderByDesc(Arrays.asList(DictDataEntity::getDictType, DictDataEntity::getSort)));
    }

    default List<DictDataEntity> selectListByStatusAndDictType(Integer status, String dictType) {
        return selectList(new LambdaQueryWrapperX<DictDataEntity>()
                .eqIfPresent(DictDataEntity::getStatus, status)
                .eqIfPresent(DictDataEntity::getDictType, dictType));
    }

}
